import launch
import launch_ros
from launch import LaunchDescription
from launch_ros.actions import Node
from ament_index_python.packages import get_package_share_directory
import os
from launch.actions import ExecuteProcess, IncludeLaunchDescription, TimerAction
from launch.launch_description_sources import PythonLaunchDescriptionSource

def generate_launch_description():
    
    #获取功能包share路径
    urdf_package_path = get_package_share_directory('botarm_description')
    default_xacro_path = os.path.join(urdf_package_path,'urdf','Sevenbot_urdf.xacro')
    default_gazebo_path = os.path.join(urdf_package_path,'launch','gazebo_arm.launch.py')
    
    #声明一个urdf目录的参数，方便修改
    action_declare_arg_mode_path = launch.actions.DeclareLaunchArgument(
        name='model',default_value=str(default_xacro_path),description='加载的模型文件路径'
    )

    action_launch_gazebo = launch.actions.IncludeLaunchDescription(
        launch.launch_description_sources.PythonLaunchDescriptionSource(
            default_gazebo_path
        ),
    )

    action_controller_manager = Node(
        package='controller_manager',
        executable='spawner',
        output='screen',
        arguments=['joint_state_broadcaster']
    )

    action_controller_manager2 = Node(
        package='controller_manager',
        executable='spawner',
        output='screen',
        arguments=['joint_trajectory_controller']
    )

    # 在Gazebo启动后延迟启动控制器
    delayed_controllers = TimerAction(
        period=10.0,
        actions=[action_controller_manager, action_controller_manager2]
    )
    
    return launch.LaunchDescription([        
        action_declare_arg_mode_path,
        action_launch_gazebo,
        delayed_controllers,
    ])